<template>
  <el-dialog
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :title="!dataForm.id ? $t('add') : $t('update')"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      @keyup.enter.native="dataFormSubmitHandle()"
      label-width="120px"
      ref="dataForm"
      v-loading="loading"
    >
      <el-form-item
        :label="$t('monitor.ip')"
        prop="ip">
        <el-select
          clearable
          filterable
          :placeholder="$t('monitor.ip')"
          v-model="dataForm.ip"
        >
          <el-option
            :key="item.id"
            :label="item.label"
            :value="item.value"
            v-for="item in ipList"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        :label="$t('monitor.port')"
        prop="port">
        <el-input
          :placeholder="$t('monitor.port')"
          v-model="dataForm.port"
        />
      </el-form-item>

      <el-form-item
        :label="$t('monitor.url')"
        prop="url">
        <el-input
          :placeholder="$t('monitor.url')"
          v-model="dataForm.url"
        />
      </el-form-item>

      <el-form-item
        :label="$t('monitor.apiName')"
        prop="name">
        <el-input
          :placeholder="$t('monitor.apiName')"
          v-model="dataForm.name"
        />
      </el-form-item>
      <el-form-item
        :label="$t('monitor.monitorThreshold')"
        prop="monitorThreshold"
      >
        <el-input-number
          v-model="dataForm.monitorThreshold"
          :min="500"
          :max="5000"
          :step="100"
        />
        <!--        <el-input-->
        <!--          type="number"-->
        <!--          :placeholder="$t('monitor.monitorThreshold')"-->
        <!--          v-model="dataForm.monitorThreshold"-->
        <!--        ></el-input>-->
      </el-form-item>

      <el-form-item
        :label="$t('monitor.method')"
        prop="method">
        <el-select
          :placeholder="$t('monitor.method')"
          v-model="dataForm.method"
        >
          <el-option
            :key="item.value"
            :label="item.label"
            :value="item.value"
            v-for="item in requestMethods"
          />
        </el-select>
      </el-form-item>

      <el-form-item
        :label="$t('monitor.params')"
        prop="params">
        <el-input
          :placeholder="$t('monitor.params')"
          type="textarea"
          v-model="dataForm.params"
        />
      </el-form-item>
      <el-form-item
        :label="$t('monitor.header')"
        prop="header">
        <el-input
          :placeholder="$t('monitor.header')"
          type="textarea"
          v-model="dataForm.header"
        />
      </el-form-item>

      <el-form-item
        :label="$t('monitor.status')"
        prop="header">
        <el-radio-group v-model="dataForm.status">
          <el-radio
            :label="true"
            border>有效</el-radio>
          <el-radio
            :label="false"
            border>无效</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button
        :loading="requesting"
        @click="visible = false">{{
          $t("cancel")
        }}</el-button>
      <el-button
        :loading="requesting"
        @click="dataFormSubmitHandle()"
        type="primary"
      >{{ $t("confirm") }}
      </el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
import { RequestMethod } from '@/utils/consts'

export default {
  name: 'ApiAddOrUpdate',
  data () {
    return {
      visible: false,
      loading: false,
      requesting: false,
      ipList: [],
      requestMethods: [],
      dataForm: {
        id: 0,
        monitorThreshold: 0,
        header: '',
        ip: '',
        port: 8080,
        method: RequestMethod.GET, // 1:GET 2:POST 3:HEAD 4:PUT 5:DELETE 6:CONNECT 7:OPTIONS 8:TRACE'
        params: '',
        url: '',
        name: '',
        status: true // 1有效0无效
      }
    }
  },
  computed: {
    dataRule () {
      return {
        ip: [
          {
            required: true,
            message: this.$t('validate.required'),
            trigger: 'blur'
          }
        ],
        port: [
          {
            required: true,
            message: this.$t('validate.required'),
            trigger: 'blur'
          }
        ],
        method: [
          {
            required: true,
            message: this.$t('validate.required'),
            trigger: 'blur'
          }
        ],
        url: [
          {
            required: true,
            message: this.$t('validate.required'),
            trigger: 'blur'
          }
        ],
        name: [
          {
            required: true,
            message: this.$t('validate.required'),
            trigger: 'blur'
          }
        ]
      }
    }
  },
  beforeMount () {
    this.requestMethods = Object.keys(RequestMethod).map(key => {
      return {
        label: key,
        value: RequestMethod[key]
      }
    })
  },
  mounted () {},
  methods: {
    init () {
      this.visible = true

      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.getIpList()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    getIpList () {
      this.loading = true

      this.$http
        .get('/monitor/mmonitorapilist/iplist')
        .then(({ data: res }) => {
          this.ipList = res.data.map(item => {
            return { id: item.id, label: item.ip, value: item.ip }
          })
        })
        .finally(() => {
          this.loading = false
        })
    },
    // 获取信息
    getInfo () {
      this.loading = true
      this.$http
        .get(`/monitor/mmonitorapilist/${this.dataForm.id}`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }

          this.dataForm = {
            ...this.dataForm,
            ...res.data
          }
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false
        })
    },

    dataFormSubmitHandle: debounce(
      function () {
        this.$refs['dataForm'].validate(valid => {
          if (!valid) {
            return false
          }
          if (this.requesting) {
            return
          }
          let params = {
            ...this.dataForm
            // status: this.dataForm.status ? 1 : 0
          }

          this.requesting = true
          this.$http[!this.dataForm.id ? 'post' : 'put'](
            '/monitor/mmonitorapilist/',
            params
          )
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg)
              }
              this.$message({
                message: this.$t('prompt.success'),
                type: 'success',
                duration: 500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
            .catch(() => {})
            .finally(() => {
              this.requesting = false
            })
        })
      },
      1000,
      { leading: true, trailing: false }
    )
  }
}
</script>

<style scoped></style>
